import { defineStore } from 'pinia'
import { getUserMenuFunction } from '/@/api/sys/v1/auth'
// import apiUser from '/@/api/v1/user'
import type { RouteRecordRaw } from 'vue-router'

/**
 * 组装面包屑
 * @param {object} urlList 菜单数组
 * @param {object} nameList 菜单数组
 * @param {Array} data 菜单数组
 * @param {Array} parentBreadcrumbs 上级面包屑
 */
function initMenuBreadcrumbChild(
  urlList: Record<string, RouteRecordRaw[]>,
  nameList: Record<string, string>,
  data: RouteRecordRaw[],
  parentBreadcrumbs: RouteRecordRaw[]
) {
  for (const item of data) {
    if (!item.path) {
      item.path = '/'
    }
    if (!item.meta) {
      item.meta = {}
    }
    // if (!item.meta.breadcrumbs) {
    //   item.meta.breadcrumbs = []
    // }
    if (!item.children) {
      item.children = []
    }

    // 把自己合并到父数据, 复制合并一份出来
    const breadcrumbs = [...parentBreadcrumbs, item] as RouteRecordRaw[]

    urlList[item.path] = breadcrumbs
    if (item.children.length > 0) {
      // 如果有子页面, 往下传递
      initMenuBreadcrumbChild(urlList, nameList, item.children, breadcrumbs)
    } else {
      let name = ''
      for (const temporary of breadcrumbs) {
        if (!temporary.meta) {
          temporary.meta = {}
        }
        if (!temporary.meta.title) {
          temporary.meta.title = ''
        }
        name = (name ? `${name} > ${temporary.meta.title}` : temporary.meta.title) as string
      }
      nameList[name] = item.path
    }
  }
}

/**
 * 路由列表
 * @function setRoutes 设置路由数据
 * @function setColumnsMenuHover 设置分栏布局菜单鼠标移入 boolean
 * @function setColumnsNavHover 设置分栏布局最左侧导航鼠标移入 boolean
 */
export const useRoutes = defineStore('routes', {
  state: (): RoutesState => ({
    routes: [],
    routeMap: {},
    secondRoutes: [],
    breadcrumbMap: {},
    breadcrumbList: [],
    authList: []
    // isColumnsMenuHover: false,
    // isColumnsNavHover: false
  }),
  actions: {
    /**
     * 从后端获取路由列表
     * @returns 列表
     */
    async getRoutes() {
      const res = await getUserMenuFunction()
      if (!res.success) {
        return []
      }
      this.authList = res.data.functionList || []
      return res.data.menuList || []
    },

    /**
     * 设置路由数据
     * @param data routes
     */
    setRoutes(data: RouteRecordRaw[]) {
      this.routes = data
    },

    /**
     * @description 设置一级路由对应的二级路由
     */
    setRouteMap() {
      if (this.routes.length === 0 || !this.routes[0].children) {
        return
      }
      const routeMap: Record<string, RouteRecordRaw[]> = {}
      for (const route of this.routes[0].children) {
        if (route.meta?.isHide) {
          routeMap[route.path] = []
          continue
        }
        if (route.children && route.children.length > 0) {
          // 移除隐藏的菜单
          routeMap[route.path] = route.children.filter(
            (item: RouteRecordRaw) => !item.meta?.isHide
          )
        } else {
          routeMap[route.path] = [route]
        }
      }
      this.routeMap = routeMap
    },

    /**
     * 根据一级路由url获取children
     * @param url 一级路由url
     */
    setSecondRoutes(url: string) {
      this.secondRoutes = this.routeMap[url] || []
    },
    setBreadcrumbMap() {
      const urlList = {},
        nameList = {}
      initMenuBreadcrumbChild(urlList, nameList, this.routes[0].children, [])

      // state.urlList = urlList
      // state.nameList = nameList
      this.breadcrumbMap = urlList
    },
    /**
     * 根据一级路由url获取children
     * @param url 一级路由url
     */
    setBreadcrumbList(url: string) {
      this.breadcrumbList = this.breadcrumbMap[url] || []
    }
    // async setColumnsMenuHover(bool: boolean) {
    //   this.isColumnsMenuHover = bool
    // },
    // async setColumnsNavHover(bool: boolean) {
    //   this.isColumnsNavHover = bool
    // }
  }
})
